package com.syxx.oa.web.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.syxx.oa.domain.ApproveInfo_1;
import com.syxx.oa.service.ApproveService1;
import com.test.jbpm.web.action.BaseAction;

@Controller
@Scope("prototype")
public class ApproveActionNew extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ApproveService1 approveService1;
	
	private ApproveInfo_1 approveInfo1;

	public ApproveInfo_1 getApproveInfo1() {
		return approveInfo1;
	}

	public void setApproveInfo1(ApproveInfo_1 approveInfo1) {
		this.approveInfo1 = approveInfo1;
	}
	
	@Autowired
	public void setApproveService1(ApproveService1 approveService1) {
		this.approveService1 = approveService1;
	}
	
	
	public String add() throws Exception {
		approveInfo1.setApprover(currentEmployee());
		approveService1.add(approveInfo1,true);
		return ajaxOut("{success: true}");
	}
	
	public String del() throws Exception {
		approveInfo1.setApprover(currentEmployee());
		approveService1.add(approveInfo1,false);
		return ajaxOut("{success: true}");
	}
	
	public String history()  throws Exception {
		List<ApproveInfo_1> approveInfoList = approveService1.findHistoryByTroubleTicketId(approveInfo1.getPerReq().get_id());
		StringBuilder sbJson = new StringBuilder();
//		sbJson.append("[");
		String empName;
		JSONArray jlist = new JSONArray();
		for (ApproveInfo_1 app : approveInfoList) {
			if(app.getPerReq() != null){
				app.setPerReq(null);
			}
			empName = "";
			if(app.getApprover() != null){
				empName = app.getApprover().getEmpName();
				app.setApprover(null);
			}
			JSONObject ob = JSONObject.fromObject(app);
			ob.put("approver", empName);
			jlist.add(ob);
		}
		sbJson.append(jlist.toString());
//		for (int i=0; i<approveInfoList.size(); i++) {
//			ApproveInfo_1 approveInfo = approveInfoList.get(i);
//			sbJson.append("{");
//			sbJson.append("desc:'");
//			sbJson.append(approveInfo.getDesc());
//			sbJson.append("',approveTime:'");
//			sbJson.append(approveInfo.getApproveTime());
//			sbJson.append("',approver:'");
//			sbJson.append(approveInfo.getApprover().getEmpName());
//			sbJson.append("'}");
//			if (i <(approveInfoList.size() - 1)) {
//				sbJson.append(",");
//			}
//		}
//		sbJson.append("]");
		return ajaxOut(sbJson.toString());
	}
	
	
}
